package client.comm.baoding.ui

import android.content.Intent
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.lifecycle.Observer
import client.comm.commlib.toast.toast
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.databinding.ActivityLoginBinding
import client.comm.baoding.ui.vm.LoginViewModel

class LoginActivity : BaseMiddleActivity<LoginViewModel, ActivityLoginBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_login
    }

    override fun initView() {
        binding?.apply {
            vm = viewModel
        }
    }

    override fun observerUI() {
        viewModel.auth.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("登录成功")
//                    if (data!!.status == 1) {
                    startActivity(Intent(this@LoginActivity, MainActivity::class.java))
//                    } else {
//                        startActivity(Intent(this@LoginActivity, UpGradeActivity::class.java))
//                    }
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })

        viewModel.isShowPwd.observe(this, Observer {
            if (it) {
                binding!!.etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding!!.etPwd.setSelection((binding!!.etPwd.getText().toString().trim()).length);
            } else {
                binding!!.etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding!!.etPwd.setSelection((binding!!.etPwd.getText().toString().trim()).length);
            }
        })
//        viewModel.isSelect.observe(this, Observer {
//             binding.ivSelect.isSelected
////            ivSelect.isSelected = !ivSelect.isSelected
//        })

    }

    override fun initViewModel(): LoginViewModel = createViewModel()


}